client = require "./client"
user = require "./user"
admin = require "./admin"
web = require "./web"
db = require "../db"
Router = require "koa-router"

route = new Router()

# 机器人接口

route.get "/", (ctx) ->
  console.log process.env.LANDINGPAGE
  console.log 'new '
  ctx.redirect process.env.LANDINGPAGE or "/admin/intro"

route.get "/w/:shortid", (ctx) ->
  data = await db.SHORTLINK.findOne shortid: ctx.params.shortid
  ctx.redirect data.url

route.get "/restart", admin.reboot

route.get "/web/update", web.updatePage
route.get "/web/help", web.helpPage
route.get "/web/index", web.indexPage
# 商家端
route.get "/admin/login", admin.login
route.get "/admin/intro", client.weixinBridge, admin.intro
route.post "/admin/signin", admin.signin
route.post "/admin/reboot", admin.reboot
route.get "/admin/manage", admin.checkAdmin, admin.manage
route.get "/admin/manage/contact", admin.checkAdmin, admin.manageContact
route.get "/admin/home", admin.checkAdmin, admin.home
route.get "/admin/setting", admin.checkAdmin, admin.settingPage
# route.get "/admin/index", admin.checkAdmin, admin.index
route.get "/admin/rooms", admin.checkAdmin, admin.roomsPage
route.get "/admin/room/:roomid", admin.checkAdmin, admin.roomPage
route.get "/admin/clients", admin.checkAdmin, admin.clientsPage
route.get "/admin/orders", admin.checkAdmin, admin.ordersPage
route.get "/admin/sends", admin.checkAdmin, admin.sendsPage
route.get "/admin/contact/:contactid", admin.checkAdmin, admin.contactPage
route.get "/admin/page/redeems", admin.checkAdmin, admin.redeemsPage
route.get "/admin/redeems", admin.checkAdmin, admin.getRedeems
route.get "/admin/page/redeem_new", admin.checkAdmin, admin.redeemNewPage
route.post "/admin/redeem/generate", admin.checkAdmin, admin.redeemGenerate
route.get "/admin/redeems/export", admin.checkAdmin, admin.redeemExport

route.get "/admin/bot/loginqr", admin.checkAdmin, admin.getLoginQr
route.get "/admin/bot/loginstatus" , admin.getLoginStatus
route.post "/admin/bot/logout", admin.botLogout
route.get "/admin/bot/info", admin.botInfo
route.post "/admin/bot", admin.checkAdmin, admin.saveBot
route.get "/admin/livetest", admin.livetest
route.get "/admin/remonk", admin.remonk

route.get "/admin/bot/stat", admin.checkAdmin, admin.getStat
route.get "/admin/bot/clients", admin.checkAdmin, admin.getClients
route.post "/admin/client/:clientid/redfee", admin.checkAdmin, admin.setClientRedFee
route.get "/admin/bot/orders", admin.checkAdmin, admin.getOrders
route.get "/admin/bot/sends", admin.checkAdmin, admin.getSends
route.get "/admin/bot/contacts", admin.checkAdmin, admin.getContacts
route.get "/admin/bot/contacts/sync", admin.checkAdmin, admin.syncContacts
route.get "/admin/bot/rooms", admin.checkAdmin, admin.getRooms
route.get "/admin/bot/rooms/sync", admin.checkAdmin, admin.syncRooms
route.post "/admin/bot/room/setclient", admin.checkAdmin, admin.setRoomClient
route.post "/admin/setting/env", admin.checkAdmin, admin.saveSettingEnv
route.post "/admin/setting/main", admin.checkAdmin, admin.saveSettingMain
route.post "/admin/uploadpfx", admin.checkAdmin, admin.uploadPfx
route.post "/admin/uploadqr", admin.checkAdmin, admin.uploadQr
route.post "/admin/uploadverify", admin.checkAdmin, admin.uploadVerify
route.post "/admin/setting/main", admin.checkAdmin, admin.saveSettingMain
route.post "/admin/bot/room/setexpires", admin.checkAdmin, admin.setRoomExpires
route.post "/admin/upload", admin.checkAdmin, admin.uploadImg

# 客户端
route.get "/client/logout", (ctx) ->
  ctx.session.openid = null
  ctx.session.user = null
  ctx.body = "登出成功"
route.get "/client/403", (ctx) ->
  ctx.render "/client/403"

route.get "/client/redirectHome", client.getOpenid, client.weixinBridge, client.checkClient, client.redirectHome
route.get "/client/bind/:contactid", client.getOpenid, client.bind
route.get "/client/home", client.getOpenid, client.weixinBridge, client.checkClient, client.home
route.get "/client/home2", client.getOpenid, client.weixinBridge, client.checkClient, client.home2
route.get "/client/page/item", client.getOpenid, client.checkClient, client.itemPage
route.get "/client/page/item/:itemid", client.getOpenid, client.checkClient, client.itemPage
route.get "/client/page/draw", client.getOpenid, client.checkClient, client.drawPage
route.get "/client/page/draw/:drawid", client.getOpenid, client.checkClient, client.drawPage
route.get "/client/info", client.getOpenid, client.checkClient, client.getClient
route.get "/client/users", client.getOpenid, client.checkClient, client.usersPage
route.get "/client/getUsers", client.getOpenid, client.checkClient, client.getUsers
route.get "/client/user/:contactid", client.getOpenid, client.checkClient, client.userPage
route.get "/client/user/:contactid/logs", client.getOpenid, client.checkClient, client.getUserLogs
route.post "/client/clear/points", client.getOpenid, client.checkClient, client.clearPoints
route.get "/client/charges", client.getOpenid, client.checkClient, client.chargesPage
route.get "/client/getCharges", client.getOpenid, client.checkClient, client.getCharges
route.get "/client/pointOrders", client.getOpenid, client.checkClient, client.pointOrdersPage
route.get "/client/getPointOrders", client.getOpenid, client.checkClient, client.getPointOrders
route.get "/client/rooms", client.getOpenid, client.checkClient, client.getRooms
route.get "/client/activerooms", client.getOpenid, client.checkClient, client.getActiveRooms
route.post "/client/room/try", client.getOpenid, client.checkClient, client.tryRoom
route.post "/client/room/charge", client.getOpenid, client.checkClient, client.chargeRoom
route.post "/client/room/redeem", client.getOpenid, client.checkClient, client.redeemRoom
route.post "/client/room/charge/callback", client.chargeRoomCallback
route.post "/client/red/charge", client.getOpenid, client.checkClient, client.chargeRed
route.post "/client/red/charge/callback", client.chargeRedCallback
route.post "/client/setHome", client.getOpenid, client.checkClient, client.setHome
route.post "/client/room/remove", client.getOpenid, client.checkClient, client.removeRoom
route.post "/client/room/point/rule", client.getOpenid, client.checkClient, client.setPointRule
route.get "/client/point/items", client.getOpenid, client.checkClient, client.getPointItems
route.get "/client/point/item/:itemid", client.getOpenid, client.checkClient, client.getPointItem
route.post "/client/point/item", client.getOpenid, client.checkClient, client.setPointItem
route.post "/client/point/item/delete", client.getOpenid, client.checkClient, client.deletePointItem

route.get "/client/point/draws", client.getOpenid, client.checkClient, client.getPointDraws
route.post "/client/point/draw", client.getOpenid, client.checkClient, client.setPointDraw
route.post "/client/point/draw/delete", client.getOpenid, client.checkClient, client.deletePointDraw

route.post "/client/upload", client.getOpenid, client.checkClient, client.uploadImg
route.post "/client/user/:contactid/savepoint", client.getOpenid, client.checkClient, client.saveUserPoint

route.get "/client/page/setting", client.getOpenid, client.checkClient, client.settingPage
route.get "/client/setting", client.getOpenid, client.checkClient, client.getSetting
route.post "/client/setting", client.getOpenid, client.checkClient, client.saveSetting
route.post "/client/setting/reset", client.getOpenid, client.checkClient, client.resetSetting

route.get "/client/page/sendlist", client.getOpenid, client.checkClient, client.sendListPage
route.get "/client/page/messages", client.getOpenid, client.checkClient, client.getMessagesPage

# route.get "/client/page/send/:sendid", client.getOpenid, client.checkClient, client.sendDetailPage

route.get "/client/page/sendform", client.getOpenid, client.checkClient, client.sendFormPage
route.get "/client/page/sendform/:sendid", client.getOpenid, client.checkClient, client.sendFormPage
route.get "/client/send/:sendid", client.getOpenid, client.checkClient, client.getSend
route.post "/client/send", client.getOpenid, client.checkClient, client.saveSend
route.post "/client/send/:sendid/remove", client.getOpenid, client.checkClient, client.removeSend
route.post "/client/send/:sendid/dosend", client.getOpenid, client.checkClient, client.doSend

route.get "/client/messages", client.getOpenid, client.checkClient, client.getMessages
route.get "/client/msglist", client.getOpenid, client.checkClient, client.getMsgList
route.get "/client/msg/:msgid", client.getOpenid, client.checkClient, client.getMsgDetail
# route.post "/client/messages/remove", client.getOpenid, client.checkClient, client.removeMessage
route.post "/client/message/delete", client.getOpenid, client.checkClient, client.deleteMessage

# 用户端
route.get "/user/fake", user.loginFake
route.get "/user/client/:clientid", user.login, user.home
route.get "/user/client/:clientid/draw/:drawid", user.login, user.drawPage
route.get "/user/client/:clientid/page/orders", user.login, user.ordersPage
route.get "/user/client/:clientid/page/logs", user.login, user.logsPage
route.get "/user/client/:clientid/page/rank", user.login, user.rankPage
route.get "/user/client/:clientid/pointItems", user.login, user.getPointItems
route.get "/user/client/:clientid/pointorders", user.login, user.getPointOrders
route.get "/user/client/:clientid/redorders",  user.login,  user.getRedOrders
route.get "/user/client/:clientid/pointlogs", user.login, user.getPointLogs
route.get "/user/client/:clientid/ranks", user.login, user.getRanks
route.post "/user/client/:client/submitaddress", user.login, user.submitAddress
route.post "/user/client/:client/checkorder",  user.checkOrder

route.post "/user/consume/:itemid", user.login, user.consume
route.post "/user/clearcache", user.clearcache

route.get "/user/bind", user.bind

route.post "/user/consumedraw/:drawid", user.login, user.consumeDraw

# route.post "/user/openred/:itemid", user.login, user.openred

module.exports = route
